package com.image.demo;

import com.image.ImageClient;
import com.image.common_utils.CommonFileUtils;
import com.image.request.GeneralDetectRequest;
import com.image.request.TagDetectRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Demo2 {

	public static void main(String[] args) {
		int appId = 1255370023;// YOUR_APPID
		String secretId = "AKIDqBRXTH8JXcVEl6uNEtMTRK3d2MaeREk4";
		String secretKey = "GuVvENK5e6QZZO1So263qRq381hIhuy2";

		// ImageClient
		ImageClient imageClient = new ImageClient(appId, secretId, secretKey);

		// 设置要操作的bucket
		String bucketName = "renzheng";
		String ret;

		///////////////////////////////////////////////////////////////
		// 黄图识别操作 //
		///////////////////////////////////////////////////////////////
		// 1. url方式
		System.out.println("====================================================");
		// String[] pornUrlList = new String[1];
		// pornUrlList[0] =
		// "https://mc.qcloudimg.com/static/img/42c0b2c4f07d3815475e02d2201c9902/image.png";
		String url = "https://mc.qcloudimg.com/static/img/42c0b2c4f07d3815475e02d2201c9902/image.png";
		// pornUrlList[1] =
		// "https://mc.qcloudimg.com/static/img/42c0b2c4f07d3815475e02d2201c9902/image.png";
		// pornUrlList[2] =
		// "https://mc.qcloudimg.com/static/img/42c0b2c4f07d3815475e02d2201c9902/image.png";
		GeneralDetectRequest generalReq = new GeneralDetectRequest(bucketName, url);

		ret = imageClient.generalDetect(generalReq);
		System.out.println("porn detect ret:" + ret);

		// 2. 图片内容方式
		System.out.println("====================================================");
		byte[] tagImage = { 0 };
		try {
			InputStream inputStream = new FileInputStream(new File("D://123.png"));
			tagImage = CommonFileUtils.getFileContentByte(inputStream, 0, inputStream.available());
		} catch (Exception ex) {
			Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
		}

		TagDetectRequest tagReq = new TagDetectRequest(bucketName, tagImage);
		ret = imageClient.tagDetect(tagReq);
		System.out.println("tag detect ret:" + ret);

	}

	// public String httpClientUploadFile(MultipartFile file) {
	//
	// final String remote_url =
	// "http://recognition.image.myqcloud.com/ocr/general";// 第三方服务器请求地址
	//
	// Credentials cred = new Credentials(WzsbSetting.APPID, WzsbSetting.SECRET_ID,
	// WzsbSetting.SECRET_KEY);
	// ClientConfig clientConfig = new ClientConfig();
	// String authorization = "";
	//
	// try {
	//
	// authorization = appSign(cred, WzsbSetting.BUCKET_NAME,
	// clientConfig.getSignExpired());
	//
	// } catch (AbstractImageException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// CloseableHttpClient httpClient = HttpClients.createDefault();
	// String result = "";
	// try {
	// String fileName = file.getOriginalFilename();
	// String end = fileName.substring(fileName.indexOf(".") + 1,
	// fileName.length());
	//
	// HttpPost httpPost = new HttpPost(remote_url);
	// MultipartEntityBuilder builder = MultipartEntityBuilder.create();
	// httpPost.setHeader("Authorization", authorization);
	//
	// if ("jpg".equals(end)) {
	// builder.addBinaryBody("image", file.getInputStream(),
	// ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
	// } else if ("gif".equals(end)) {
	// builder.addBinaryBody("image", Util.getGifToJpg(file.getInputStream()),
	// ContentType.MULTIPART_FORM_DATA,
	// fileName);// 文件流
	// } else {
	// return "";
	// }
	//
	// builder.addTextBody("appid", 1255370023 + "");// 类似浏览器表单提交，对应input的name和value
	// builder.addTextBody("bucket", "renzheng");// 类似浏览器表单提交，对应input的name和value
	// HttpEntity entity = builder.build();
	// httpPost.setEntity(entity);
	// HttpResponse response = httpClient.execute(httpPost);// 执行提交
	// HttpEntity responseEntity = response.getEntity();
	// if (responseEntity != null) {
	// // 将响应内容转换为字符串
	// result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
	// System.out.println(result);
	// }
	// } catch (IOException e) {
	// e.printStackTrace();
	// } catch (Exception e) {
	// e.printStackTrace();
	// } finally {
	// try {
	// httpClient.close();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	// return result;
	// }

}
